<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>FireEagle</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="oauth.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="ref.oauth.html">OAuth 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="oauth.examples.html">范例</a></li>
    <li>FireEagle</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="oauth.examples.fireeagle" class="section">
  <h2 class="title">FireEagle</h2>
  <p class="para">
   <div class="example" id="example-5312">
    
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$req_url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'https://fireeagle.yahooapis.com/oauth/request_token'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$authurl&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'https://fireeagle.yahoo.net/oauth/authorize'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$acc_url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'https://fireeagle.yahooapis.com/oauth/access_token'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$api_url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'https://fireeagle.yahooapis.com/api/0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conskey&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'your_consumer_key'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conssec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'your_consumer_secret'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;&nbsp;当&nbsp;state=1&nbsp;则下次请求应该包含一个&nbsp;oauth_token&nbsp;。<br />//&nbsp;&nbsp;如果没有则返回&nbsp;0<br /></span><span style="color: #007700">if(!isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token'</span><span style="color: #007700">])&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'state'</span><span style="color: #007700">]==</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'state'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />try&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$oauth&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">OAuth</span><span style="color: #007700">(</span><span style="color: #0000BB">$conskey</span><span style="color: #007700">,</span><span style="color: #0000BB">$conssec</span><span style="color: #007700">,</span><span style="color: #0000BB">OAUTH_SIG_METHOD_HMACSHA1</span><span style="color: #007700">,</span><span style="color: #0000BB">OAUTH_AUTH_TYPE_URI</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">enableDebug</span><span style="color: #007700">();<br />&nbsp;&nbsp;if(!isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token'</span><span style="color: #007700">])&nbsp;&amp;&amp;&nbsp;!</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'state'</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$request_token_info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getRequestToken</span><span style="color: #007700">(</span><span style="color: #0000BB">$req_url</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'secret'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$request_token_info</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token_secret'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'state'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location:&nbsp;'</span><span style="color: #007700">.</span><span style="color: #0000BB">$authurl</span><span style="color: #007700">.</span><span style="color: #DD0000">'?oauth_token='</span><span style="color: #007700">.</span><span style="color: #0000BB">$request_token_info</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;}&nbsp;else&nbsp;if(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'state'</span><span style="color: #007700">]==</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setToken</span><span style="color: #007700">(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token'</span><span style="color: #007700">],</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'secret'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$access_token_info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAccessToken</span><span style="color: #007700">(</span><span style="color: #0000BB">$acc_url</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'state'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'token'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$access_token_info</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'secret'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$access_token_info</span><span style="color: #007700">[</span><span style="color: #DD0000">'oauth_token_secret'</span><span style="color: #007700">];<br />&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setToken</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'token'</span><span style="color: #007700">],</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'secret'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$api_url</span><span style="color: #DD0000">/user.json"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$oauth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getLastResponse</span><span style="color: #007700">());<br />&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">);<br />}&nbsp;catch(</span><span style="color: #0000BB">OAuthException&nbsp;$E</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$E</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div></div></div></body></html>